﻿using AutoMapper;
using MediatR;
using Provenance.Domain.DeepProcess;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.DeepProcess;
using Provenance.Read.Api.Application.Command.DeepProcess;

namespace Provenance.Read.Api.Application.CommandHandler.DeepProcess
{
    public class BackRowMaterialCommandHandler : IRequestHandler<BackRowMaterialCommand, ApiResult<RowMaterial>>
    {
        private readonly IRowMaterialRepository _rowMaterialRepository;
        private readonly ILogger<BackRowMaterialCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackRowMaterialCommandHandler(IRowMaterialRepository rowMaterialRepository, ILogger<BackRowMaterialCommandHandler> logger, IMapper mapper)
        {
            _rowMaterialRepository = rowMaterialRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<RowMaterial>> Handle(BackRowMaterialCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj=await _rowMaterialRepository.GetAsync(x=>x.ProductInfoId==request.ProductInfoId);
                return ApiResult<RowMaterial>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return ApiResult<RowMaterial>.Failure("Error retrieving back row material."+ex.Message);
            }
        }
    }
}
